package com.qingzhuge.manager.controller;

import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.file.controller.BaseDownAndloadFileController;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.qingzhuge.common.Constants.Controller.*;

/**
 * @author : zero.xiao
 * @description :
 * @date :2018/5/4 0004 下午 16:22
 * @modified :
 */
@Api(tags = "A文件上传下载")
@AResource(name = "上传下载", type = EResourceType.menu, catalog = "系统管理")
@RestController
@RequestMapping("api")
@CrossOrigin
public class SysFileUploadController extends BaseDownAndloadFileController {

    /**
     * 会员、商品文件可以自由下载
     *
     * @return Integer
     */
    @Override
    protected Integer getFileAuthType() {
        return 0;
    }


    @Override
    @PostMapping("sys/auth/" + PATH_FILE + "/upload/{module}/{type}")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('UPLOAD')")
    public ResponseEntity<ResponseBodyDto<Object>> upload(MultipartFile file,
                                                          @PathVariable String module,
                                                          @PathVariable String type) {
        return super.upload(file, module, type);
    }

    @Override
    @GetMapping("sys/open/" + PATH_FILE + "/" + PATH_DOWNLOAD)
    public void download(@PathVariable Long id) {
        super.download(id);
    }

    @Override
    @GetMapping("sys/open/" + PATH_FILE + "/{module}/{type}/{date}/{name}.{suf}")
    public void download(@PathVariable String module,
                         @PathVariable String type,
                         @PathVariable String date,
                         @PathVariable String name,
                         @PathVariable String suf) {
        super.download(module, type, date, name, suf);
    }

    @Override
    @GetMapping("sys/open/" + PATH_FILE + "/" + PATH_BY_ID)
    public ResponseEntity<ResponseBodyDto<Object>> file(@PathVariable Long id) {
        return super.file(id);
    }
}
